package com.rabbitmq.learn.tx;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.learn.common.ConnectionUtils;
import com.rabbitmq.learn.common.RabbitMqConstant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by Lxk on 2019/8/14.
 */
public class Sender {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection con = ConnectionUtils.getConnection();

        Channel channel = con.createChannel();

        channel.queueDeclare(RabbitMqConstant.TxSimple.QUEUE_TX_SIMPLE,false,false,false,null);

        String msg = "test tx...";

        try{
            channel.txSelect();

            channel.basicPublish("", RabbitMqConstant.TxSimple.QUEUE_TX_SIMPLE,null,msg.getBytes());
            int res = 1/0;
            channel.txCommit();
        }catch (Exception e){
            channel.txRollback();
        }

        channel.close();

        con.close();
    }

}
